package se.webbzon.oschi01.gui;

import java.awt.Color;
import java.awt.Component;
import java.io.IOException;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellRenderer;

import se.webbzon.oschi01.nbt.Tag;
import se.webbzon.oschi01.singleton.Singletons;

public class NBTTreeCellRenderer extends DefaultTreeCellRenderer {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -2028457689227109420L;
	
	private static final Color borderColor = new Color(99,130,191);
	
	private final NBTIconSheet iconLoader;
	
	public NBTTreeCellRenderer() throws IOException {
		iconLoader = Singletons.instance(NBTIconSheet.class);
	}

	
	@Override public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean exp, boolean leaf, int row, boolean hasFocus) {
		Icon icon;
		if (value instanceof Tag) {
			Tag<?> t = (Tag<?>) value;
			icon = iconLoader.getIcon(t.getFactory().getId());
			setLeafIcon(icon);
			setOpenIcon(icon);
			setClosedIcon(icon);
		} else {
			setLeafIcon(getDefaultLeafIcon());
			setOpenIcon(getDefaultOpenIcon());
			setClosedIcon(getDefaultClosedIcon());
		}
		
		if (sel)
			setBorder(BorderFactory.createLineBorder(borderColor));
		else
			setBorder(BorderFactory.createEmptyBorder());
		
		super.getTreeCellRendererComponent(tree, value, sel, exp, leaf, row, hasFocus);
		return this;
	}

}
